<?php


namespace app\engine\controller;


use cmf\controller\HomeBaseController;
use think\Db;

class EmpController extends HomeBaseController
{
    public function index()
    {
        $data = Db::name('emp')->order('date desc')->select()->toArray();
        foreach ($data as $k => &$v) {
            $v['sud'] = Db::name('staff')->where(['id' => $v['uid']])->value('income');
            $v['lname'] = Db::name('staff')->where(['id' => $v['uid']])->value('name');
            $v['ltype'] = Db::name('staff')->where(['id' => $v['uid']])->value('position');
            $v['pop'] = $v['sud'] - $v['ded'] - $v['pay'];
        }
        $all = Db::name('emp')->group('uid')->field('uid')->select()->toArray();
        foreach ($all as $k => &$v) {
            $v['lname'] = Db::name('staff')->where(['id' => $v['uid']])->value('name');
//            $v['all'] = Db::name('emp')->where(['uid' => $v['uid']])->count() * Db::name('staff')->where(['id' => $v['uid']])->value('income');
            $v['ded'] = Db::name('emp')->where(['uid' => $v['uid']])->sum('ded');
            $v['pay'] = Db::name('emp')->where(['uid' => $v['uid']])->sum('pay');
//            $v['cans'] = $v['all'] - $v['ded'] - $v['pay'];
        }
        $this->assign('all', $all);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2){
            $this->error('无权限操作');
        }
        $name = Db::name('staff')->field('id,name')->select();
        $type = Db::name('staff')->field('id,position')->select();
        $this->assign('name', $name);
        $this->assign('type', $type);
        return $this->fetch();
    }

    public function doadd()
    {
        $data = $this->request->param();
        $files = $this->request->file('img');
        unset($data['img']);
        if (empty($data['date'])) {
            $this->error('请选择时间');
        }
        if($files){
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['img'] = implode(',', $kps);
        }
        $res = Db::name('emp')->insert($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('emp')->where(['id' => $data['id']])->find();
        $res['lname'] = Db::name('staff')->where(['id' => $res['uid']])->value('name');
        $res['ltype'] = Db::name('staff')->where(['id' => $res['uid']])->value('position');

        $res['img'] = explode(',', $res['img']);
        foreach ($res['img'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function doedit()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        $files = $this->request->file('img');
        if ($files) {
            $check = Db::name('emp')->where(['id' => $id])->find();
            if ($check['img']) {
                $img = 'http://' . $_SERVER['SERVER_NAME'] . $check['img'];
                if (file_exists($img)) {
                    unlink($img);
                }
            }
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['img'] = implode(',', $kps);
        }
        $res = Db::name('emp')->where(['id' => $id])->update($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function del()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $check = Db::name('emp')->where(['id' => $data['id']])->find();
        if ($check['img']) {
            $img = CMF_ROOT . $check['img'];
            if (file_exists($img)) {
                unlink($img);
            }
        }
        $res = Db::name('emp')->where(['id' => $data['id']])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }
}